package py.com.alfa.escriba.gui.session;

import java.io.Serializable;
import java.security.Principal;

import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SystemSessionManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1091923101093763001L;
	private static final Logger logger = LoggerFactory.getLogger(SystemSessionManager.class);
	
	public void init(AuthenticationInfo user) throws Exception {
		try{
		  	if(user != null){
				authorize(user.getUsername(), user.getPassword());
		  	}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw e;
		}
	}	

	public void init(HttpServletRequest request){
		logger.info("\n==============\nSession Manager\n==============\n");
	  	Principal user = request.getUserPrincipal();
	  	if(user != null){
			try {
				authorize(user.getName(), user.getName());
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
	  	}
	}	
	@PreDestroy
	public void destroy(){
		logger.info("\n==============\nDestroying System Data\n==============\n");
	}	
	
	private void authorize(String username, String password) throws Exception {
		Subject currentUser = SecurityUtils.getSubject();
		if (!currentUser.isAuthenticated()) {
			UsernamePasswordToken token;
			token = new UsernamePasswordToken(username, password);
			//token.setRememberMe(true);
			currentUser.login(token);

		}
	}	
}
